<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\room;


use app\common\enum\DeviceEnum;
use app\common\model\BaseModel;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\Device;
use app\common\model\user\User;


/**
 * 全院设备关联模型
 * Class RoomHospitalDevice
 * @package app\common\model\room
 */
class RoomHospitalDevice extends BaseModel {

    protected $name = 'room_hospital_device';

    public function building() {
        return $this->hasOne(Building::class, 'id', 'bid');
    }

    public function campus() {
        return $this->hasOne(Campus::class, 'id', 'cid');
    }

    public function room() {
        return $this->hasOne(RoomHospital::class, 'id', 'rid')->with("building");
    }

    public function device() {
        return $this->hasOne(Device::class, 'id', 'device_id')->append(["device_type"]);
    }

    public function roomUser() {
        return $this->hasOne(RoomHospital::class, 'id', 'rid')->append([
            "user",
        ]);
    }

    public function getTotalFeeAttr($value, $data) {
        return 1 * sprintf("%.2f", $value);
    }

    public function getSortAttr($value, $data) {
        return $this->handle_sort($data['sort']);
    }

    public function getCbrIdsAttr($value, $data) {
        $bid = RoomHospital::where("id", $data['rid'])->value("bid");
        return [(int)$data['cid'], (int)$bid, (int)$data['rid']];
    }

    public function getCdIdsAttr($value, $data) {
        return [(int)$data['cid'], (int)$data['device_id']];
    }

    public function getDeviceTypeTextAttr($value, $data) {
        return DeviceEnum::getDisableDesc($data['device_type']);
    }

    public function getCopyAddAttr($value, $data) {
        // 禁用append
        return "";
    }

}
